package kr.co.casually.dialog;

import java.io.UnsupportedEncodingException;

import kr.co.casually.Me2day;
import kr.co.casually.R;
import kr.co.casually.SNSSettingActivity;
import kr.co.casually.YouTubePlayListActivity;
import kr.co.casually.kakaolink.KakaoLink;
import kr.co.casually.preference.ConcertPreferences;
import kr.co.casually.types.ConcertData;
import kr.co.casually.util.Utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.android.Facebook;

public class SendDialog extends Dialog implements OnClickListener {
	ImageView ivMe2Day;
	ImageView ivKakao;
	ImageView ivFace;
	ImageView ivYoutube;
	ImageView ivCancel;
	Context mContext;
	ConcertData mConcertData;
	Activity mActivity;

	private SharedPreferences mPrefs;
	private Facebook mFacebook = new Facebook(Utils.FACEBOOK_APP_ID);

	String message = "";
	String referenceURLString = "http://culture.seoul.go.kr";
	String appPackageId = "com.example.app";
	String appVersion = "1.0";
	KakaoLink kakaoLink = null;

	public SendDialog(Context context, Activity act, ConcertData data) {
		super(context);
		mPrefs = PreferenceManager.getDefaultSharedPreferences(act);
		mContext = context;
		mActivity = act;
		mConcertData = data;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.send_dialog);
		ivFace = (ImageView) findViewById(R.id.iv_send_dialog_facebook);
		ivMe2Day = (ImageView) findViewById(R.id.iv_send_dialog_metoday);
		ivKakao = (ImageView) findViewById(R.id.iv_send_dialog_kakao);
		ivYoutube = (ImageView) findViewById(R.id.iv_send_dialog_youtube);
		ivCancel = (ImageView) findViewById(R.id.iv_send_dialog_cancel);

		ivFace.setOnClickListener(this);
		ivMe2Day.setOnClickListener(this);
		ivKakao.setOnClickListener(this);
		ivYoutube.setOnClickListener(this);
		ivCancel.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_send_dialog_facebook:
			String facebook = "서울시공연정보 \n" + mConcertData.getTitle() + "\n"
					+ mConcertData.getStartDate() + " ~ "
					+ mConcertData.getEndDate();
			feed(facebook);
			break;

		case R.id.iv_send_dialog_metoday:
			String me2day = "서울시공연정보 \n" + mConcertData.getTitle() + "\n "
					+ mConcertData.getStartDate() + " ~ "
					+ mConcertData.getEndDate();
			startMe2day(me2day, "서울시공연정보 \n");
			break;

		case R.id.iv_send_dialog_kakao:
			String kakao = "서울시공연정보 \n" + mConcertData.getTitle() + "\n"
					+ mConcertData.getStartDate() + " ~ "
					+ mConcertData.getEndDate() + "\n";
			sendMsgKakao(kakao);
			break;

		case R.id.iv_send_dialog_youtube:
			startYouTubeActivity(mConcertData.getTitle());
			break;

		case R.id.iv_send_dialog_cancel:
			dismiss();
			break;

		default:
			break;
		}
	}

	// 카카오톡 메세지 보내기..
	private void sendMsgKakao(String msg) {

		try {
			PackageInfo i = mActivity.getPackageManager().getPackageInfo(
					mActivity.getPackageName(), 0);
			appVersion = i.versionName;
			message = msg;
			appPackageId = mActivity.getApplication().getPackageName();
			kakaoLink = new KakaoLink(mActivity, referenceURLString,
					appPackageId, appVersion, message, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		if (kakaoLink.isAvailable()) {
			mActivity.startActivity(kakaoLink.getIntent());
		} else {

			// 카카오톡이 설치되어 있지 않은 경우에 대한 처리
			Toast.makeText(mActivity, mActivity.getString(R.string.kakao_not_install),
					Toast.LENGTH_LONG).show();

		}
	}

	// Facebook에 글쓰기
	private void feed(String message) {
		try {

			if (mPrefs
					.getString(ConcertPreferences.FACEBOOK_ACCESS_TOKEN, null) != null
					&& !mPrefs.getString(
							ConcertPreferences.FACEBOOK_ACCESS_TOKEN, null)
							.equals("")) {
				mFacebook.setAccessToken(mPrefs.getString(
						ConcertPreferences.FACEBOOK_ACCESS_TOKEN, null));
				Bundle params = new Bundle();
				params.putString("message", message);
				params.putString("name", "사용자명");
				params.putString("link", "");
				params.putString("description", "서울 공연정보 앱에서 포스트됨");
				params.putString("picture", "");

				mFacebook.request("me/feed", params, "POST");

				Toast.makeText(mActivity,
						mActivity.getString(R.string.facebook_write_success),
						1000).show();
			} else {

				Intent intent = new Intent(mActivity, SNSSettingActivity.class);
				mActivity.startActivity(intent);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// me2day에 글쓰기
	public void startMe2day(String keyword, String tag) {
		Intent intent = new Intent(mActivity, Me2day.class);
		intent.putExtra("keyword", keyword);
		intent.putExtra("tag", tag);
		mActivity.startActivity(intent);
	}

	private void startYouTubeActivity(String keyword) {
		Intent intent = new Intent(mActivity, YouTubePlayListActivity.class);
		intent.putExtra("keyword", keyword);
		mActivity.startActivity(intent);
	}

}
